package de.tuhrig.sportlog.helper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import de.tuhrig.sportlog.pmf.PMF;
import de.tuhrig.sportlog.pmf.queries.QueryFactory;
import de.tuhrig.sportlog.user.log.Log;

public class CalendarView {

	public String getHTML() {

		Calendar ca = new GregorianCalendar();

		int currentMonth = ca.get(Calendar.MONTH);
		int currentYear = ca.get(Calendar.YEAR);

		GregorianCalendar previous = new GregorianCalendar(currentYear, currentMonth-1, 1);
		GregorianCalendar current = new GregorianCalendar(currentYear, currentMonth, 1);
		GregorianCalendar next = new GregorianCalendar(currentYear, currentMonth+1, 1);

		StringBuilder builder = new StringBuilder();
		
		builder.append("<div id='calendaroverview'><table><tr>");
		builder.append("<td align='center'>" + (previous.get(Calendar.MONTH)+1) + " / " + previous.get(Calendar.YEAR) + "</td>");
		builder.append("<td align='center'>" + (current.get(Calendar.MONTH)+1) + " / " + current.get(Calendar.YEAR) + "</td>");
		builder.append("<td align='center'>" + (next.get(Calendar.MONTH)+1) + " / " + next.get(Calendar.YEAR) + "</td>");
		builder.append("</tr><tr>");
		builder.append("<td>");
		appendCalenderForMonth(builder, previous);	// previous month
		builder.append("</td>");
		builder.append("<td>");
		appendCalenderForMonth(builder, current);	// current month
		builder.append("</td>");
		builder.append("<td>");
		appendCalenderForMonth(builder, next);		// next month
		builder.append("</td>");
		builder.append("</tr></table></div>");
		
		return builder.toString();
	}

	private void appendCalenderForMonth(StringBuilder builder, GregorianCalendar cal) {

		int daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		int weekStartDay = cal.get(Calendar.DAY_OF_WEEK);

		UserService userService = UserServiceFactory.getUserService(); 
		User user = userService.getCurrentUser(); 
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query query = new QueryFactory(pm).getLogsFromUserIdAfterDate(); 

		@SuppressWarnings("unchecked")
		List<Log> logsAfterMonth = (List<Log>) query.execute(user.getUserId(), cal.getTime());

		builder.append("<table align='center' width='200px'>");
		builder.append("<tr>");
		builder.append("<th>Sun</th>");
		builder.append("<th>Mon</th>");
		builder.append("<th>Tue</th>");
		builder.append("<th>Wed</th>");
		builder.append("<th>Thu</th>");
		builder.append("<th>Fri</th>");
		builder.append("<th>Sat</th>");
		builder.append("</tr>");

		int cnt = 1;
		for(int i = 1; i <= 6; i++) {

			builder.append("<tr>");

			for (int j = 1; j <= 7; j++) {
				
				if (cnt < weekStartDay || (cnt - weekStartDay + 1) > daysOfMonth) {

					builder.append("<td align='center'>&nbsp;</td>");

				} 
				else {

					boolean freeDay = true;
					
					int day = cnt - weekStartDay + 1;
					int month = cal.get(Calendar.MONTH);
					int year = cal.get(Calendar.YEAR);
					
					List<Calendar> list = new ArrayList<Calendar>();
					
					for(Log log: logsAfterMonth) {
						
						Calendar tmp = Calendar.getInstance();
						tmp.setTime(log.getDate());

						if(tmp.get(Calendar.DAY_OF_MONTH) == day && tmp.get(Calendar.MONTH) == cal.get(Calendar.MONTH) && !list.contains(tmp)) {
							
							builder.append("<td align='center' bgcolor='e1ddd9'><a href='Logs.jsp?day=" + day + "&month=" + month + "&year="+ year + "'>" + day + "</a></td>");
							
							list.add(tmp);
							freeDay = false;
						}
					}
					
					if(freeDay)
						builder.append("<td align='center'>" + day + "</td>");
				}
				
				cnt++;
			}

			builder.append("</tr>");
		}

		builder.append("</table>");
	}
}